<template>
  <div>
    <el-form
        v-loading="loading"
        element-loading-text="正在登录..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        ref="loginForm"
        :model="loginForm"
        :rules="rules"
        class="loginContainer">
      <h3>系统登录</h3>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" auto-complete="false" placeholder="请输入用户名" type="text"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" auto-complete="false" placeholder="请输入密码" type="password"></el-input>
      </el-form-item>
      <el-form-item class="codeContainer" prop="code">
        <el-input v-model="loginForm.code" auto-complete="false" placeholder="请点击图片更换验证码" style="margin-right: 2px"
                  type="text"></el-input>
        <img :src="captchaUrl" @click="updateCaptcha"/>
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="checked">记住我</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button style="width: 100%;" type="primary" @click="submitLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      captchaUrl: '/captcha?time=' + new Date(),
      loginForm: {
        username: 'admin',
        password: 'admin',
        code: ''
      },
      checked: true,
      loading: false,
      rules: {
        username: [{required: true, message: '请输入用户名', trigger: 'blur'}, {
          min: 5,
          max: 12,
          message: '长度在 5 到 12 个字符',
          trigger: 'blur'
        }],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}],
        code: [{required: true, message: '请输入验证码', trigger: 'blur'}]
      }
    }
  },
  methods: {
    updateCaptcha() {
      this.captchaUrl = '/captcha?time=' + new Date();
    },
    submitLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.postResquest('/login', this.loginForm).then(res => {
            if (res) {
              //console.log(res.data.records);
              const tokenStr = res.data.records.tokenHead + res.data.records.token;
              window.sessionStorage.setItem("tokenStr",tokenStr);
              this.$router.replace('/home');
              this.loading = false;
            } else {
              this.$message.error(res.message);
              this.loading = false;
            }
          }).catch(err => {
            console.log(err);
            this.loading = false;
          });
        } else {
          this.$message.error("请输入所有要求字段！");
          return false;
        }
      });
    }
  }
}
</script>

<style lang="less">
.loginContainer {
  border-radius: 6px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 360px;
  padding: 15px 35px 15px 35px;
  background: #FFFFFF;
  border: 2px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;

  h3 {
    margin: 10px auto 30px auto;
    text-align: center;
  }

  .codeContainer > div {
    display: flex;
    align-items: center;
  }
}
</style>